package net.comm.common;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;


import org.sakaiproject.db.impl.BasicSqlService;

import net.comm.user.bean.BaseUsers;
import net.comm.user.db.DbUsersService;
import net.comm.user.db.UsersServiceSqlMysql;
import net.comm.user.service.impl.UserSysinfoServiceImpl;
import net.comm.user.service.impl.UsersServiceImpl;
import net.comm.user.web.form.UserSysinfoForm;
import net.comm.user.web.form.UsersForm;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

public class SessionCount implements HttpSessionAttributeListener 
{ 
  private static int activeSessions = 0; 
  private static HashMap<String, String> Usermap = new HashMap<String, String>();
  public void attributeAdded(HttpSessionBindingEvent   se) 
  { 
    if( GlbFinal.SESSION_USERID.equals(se.getName())) 
    { 
      String userID = (String)se.getValue(); 
      if(Usermap.get(userID) == null){
    	  
    	Usermap.put(userID, userID);
        activeSessions++; 
        Connection cn = null;
  		try
  		{
  			cn = DbcpPool.getConnection();
  			
  		      //读取会员信息
  			Object[] fields1 = new Object[1];
  			
  			fields1[0] = userID;
  						
  			UsersServiceSqlMysql servSql = new UsersServiceSqlMysql();
  			BasicSqlService  bSqlServ = new BasicSqlService();
  			
  			String sql = servSql.getbykeySql();
  						
  			List list = bSqlServ.dbRead(cn, sql, fields1, new DbUsersService());
  			if(list.size() == 0){
  				DbcpPool.closeTran(cn);
  				DbcpPool.returnConnection(cn);
  				return;				
  			}
  			BaseUsers base = (BaseUsers)list.get(0);
			DbcpPool.beginTran(cn);
  			
			//更新状态
			Object[] fields = new Object[4];
  			sql = servSql.getUpdLtimeSql();
 			
  		    //修改会员状态为在线，判断如果上次登录日期不是当前工作日， 则更新登录日期，同时增加积分
  			if(DatetimeUtil.getQuot(DatetimeUtil.getDatetime(), base.getLtime())> 0){
  	
  				fields[0] = base.getLoginCnt() + 1;
  				fields[1] = DatetimeUtil.getDatetime();
  				fields[2] = GlbFinal.YES;
  				fields[3] = base.getUserID();
  	  			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
  	  			if(!res){
  	  				DbcpPool.rollBackTran(cn);
  	  				DbcpPool.closeTran(cn);
  	  				DbcpPool.returnConnection(cn);
  	  				return;
  	  			}	  				
  				
  				UserSysinfoServiceImpl syservice  = new UserSysinfoServiceImpl();
  				ReturnMessage msg  = new ReturnMessage();			
  				UserSysinfoForm sysForm = new UserSysinfoForm();				

  				sysForm.setUserID(base.getUserID());
  				sysForm.setIntegral(1);
  				
  				syservice.update(cn, sysForm, msg);
  				if(msg.getErrCode() != 0){
  					DbcpPool.rollBackTran(cn);
  					DbcpPool.closeTran(cn);
  					DbcpPool.returnConnection(cn);
  					return;
  				}	
  			}else{  				
  				fields[0] = base.getLoginCnt();
  				fields[1] = base.getLtime();
  				fields[2] = GlbFinal.YES;
  				fields[3] = base.getUserID();	
  	  			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
  	  			if(!res){
  	  				DbcpPool.rollBackTran(cn);
  	  				DbcpPool.closeTran(cn);
  	  				DbcpPool.returnConnection(cn);
  	  				return;
  	  			}	 				
  			}
  			
			DbcpPool.commitTran(cn); 				  				
  			DbcpPool.closeTran(cn);			
  			DbcpPool.returnConnection(cn);
  			
  		} catch(Exception ex) 
  		{
  			try 
  			{
  				DbcpPool.closeTran(cn);
  				DbcpPool.returnConnection(cn);
  			} catch(Exception ex1) 
  			{
  				ex1.printStackTrace();
  			}				
  		} finally {
  			DbcpPool.returnConnection(cn);
  		}
      }
    } 
    
  } 
  public void attributeRemoved(HttpSessionBindingEvent   se) 
  { 
    if( GlbFinal.SESSION_USERID.equals(se.getName())) 
    { 
      Integer userID = (Integer)se.getValue(); 
      if(Usermap.get(userID) != null)
    	  Usermap.remove(userID);
	  if(activeSessions > 0) 
		activeSessions--; 

	  //修改users表中的登录状态为离线
  	  UsersForm theForm = new UsersForm();
  	  
  	  theForm.setUserID(userID);
  	  theForm.setOnline(GlbFinal.USER_OFFLINE);
  	  
  	  UsersServiceImpl service = new UsersServiceImpl();
  	  ReturnMessage msg = new ReturnMessage();

  	  service.update(theForm, msg);
    } 	
  } 

  public void attributeReplaced(HttpSessionBindingEvent   se){} 

  public static int getActiveSessions(){ 
	return activeSessions; 
  }
  public static HashMap<String, String> getUsermap() {
	return Usermap;
  }
  
}
